<template>
  <Dialog v-model="superVisible" :title="title">
    <el-form ref="superFormRef" :model="superForm" :rules="rules" label-width="120px">
      <el-form-item label="所属分支" prop="branch">
        <el-input v-model="superForm.branch" placeholder="请输入所属分支"></el-input>
      </el-form-item>
      <el-form-item label="分支属性" prop="branchType">
        <el-select v-model="superForm.branchType" placeholder="请选择分支属性">
          <el-option label="肯定" :value="1"> </el-option>
          <el-option label="否定" :value="2"> </el-option>
          <el-option label="拒绝" :value="3"> </el-option>
          <el-option label="默认" :value="5"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键词" prop="keys">
        <el-input
          v-model="superForm.keys"
          type="textarea"
          placeholder="请输入关键词,逗号隔开"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="superVisible = false">取消</el-button>
      <el-button type="primary" :disabled="fromDisable" @click="submit">确定</el-button>
    </template>
  </Dialog>
</template>

<script setup>
import * as defaultIvrKnowApi from '@/api/call/manage'
const superVisible = ref(false)
const title = ref('知识库问题')
const superForm = reactive({
  id: '',
  branch: '',
  branchType: '',
  keys: '',
  type: 1
})
const rules = reactive({
  branch: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
  branchType: [{ required: true, message: '分支属性不能为空', trigger: 'change' }]
})
const open = (type, idVal) => {
  superVisible.value = true
  if (idVal) {
    title.value = '编辑知识库问题'
    getDetail(idVal)
  } else {
    superForm.id = ''
    title.value = '创建知识库问题'
    resetForm()
  }
}
const getDetail = async (idVal) => {
  const { id, branch, keys, type, branchType } = await defaultIvrKnowApi.getIvrKeyword(idVal)
  Object.assign(superForm, {
    id,
    branch,
    keys,
    type,
    branchType
  })
}
const resetForm = () => {
  superForm.branch = ''
  superForm.keys = ''
  superForm.type = ''
  superForm.branchType = ''
}
const superFormRef = ref(false)
const fromDisable = ref(false)
const emit = defineEmits(['success'])
const submit = async () => {
  try {
    await superFormRef.value.validate()
    fromDisable.value = true
    if (superForm.id) {
      await defaultIvrKnowApi.updateIvrKeyword(superForm)
    } else {
      await defaultIvrKnowApi.createIvrKeyword(superForm)
    }
    ElMessage.success('操作成功')
    superVisible.value = false
    emit('success')
  } finally {
    fromDisable.value = false
  }
}
defineExpose({ open })
</script>

<style lang="scss" scoped></style>
